package windows
{
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.*
	
	/**
	 * ...
	 * @author yaempechenki@gmail.com
	 */
	public class SketchHolder extends Sprite
	{
		private var imageLoader:Loader;
		private var fileReference:FileReference;
		private var container:Sprite;

		public function SketchHolder() 
		{
			this.container = new Sprite();
			addChild(container);
		}

		public function openFile():void {
            cls();
			imageLoader = new Loader();
			fileReference = new FileReference();
			var textTypeFilter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.png)", "*.jpg;*.jpeg;*.png");
			fileReference.browse([textTypeFilter]);
			fileReference.addEventListener(Event.SELECT, onFileSelected);
			fileReference.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			fileReference.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
		}
		
		public function refresh():void {
			container.scaleX = 1;	// scale
			container.scaleY = 1;
			container.x = 0;		// offset
			container.y = 0;
		}

		public function setSize(fWidth:int, fHeight:int):void {
			this.scrollRect = new Rectangle(0, 0, fWidth, fHeight);
		}
		
        private function cls():void {
            if(fileReference) {

               fileReference.removeEventListener(Event.SELECT, onFileSelected);
               fileReference.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
               fileReference.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);

               fileReference.removeEventListener(ProgressEvent.PROGRESS, onProgress);
               fileReference.removeEventListener(Event.COMPLETE, onComplete);
            }
            if(container) {
               container.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
               container.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
               container.removeEventListener(MouseEvent.MOUSE_OUT, onMouseUpHandler);
                while(container.numChildren){
                    container.removeChildAt(0);
                }
            }

            this.removeEventListener(Event.ENTER_FRAME, waitForLoad);
        }

		private function onFileSelected(evt:Event):void {
			fileReference.addEventListener(ProgressEvent.PROGRESS, onProgress);
			fileReference.addEventListener(Event.COMPLETE, onComplete);
			fileReference.load();
		}

        private function onProgress(evt:ProgressEvent):void {
            trace("Loaded " + evt.bytesLoaded + " of " + evt.bytesTotal + " bytes.");
        }

		private function onComplete(evt:Event):void {
			imageLoader.loadBytes(fileReference.data);
            container.addChild(imageLoader);

			this.addEventListener(Event.ENTER_FRAME, waitForLoad);
		}
		
		private function waitForLoad(e:Event):void {
			if(container.width>0) {
                this.removeEventListener(Event.ENTER_FRAME, waitForLoad);

                container.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
                container.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
                container.addEventListener(MouseEvent.MOUSE_OUT, onMouseUpHandler);
			}
		}
		
		/**
         * drag and drop background
        */
		private function onMouseDownHandler(event:MouseEvent):void {
			container.startDrag(false);
		}
		
		private function onMouseUpHandler(event:MouseEvent):void {
			container.stopDrag();
		}

        /**
         *  Error handlers
         */
        private function onIOError(evt:IOErrorEvent):void {
            trace("There was an IO Error.");
        }

        private function onSecurityError(evt:Event):void {
            trace("There was a security error.");
        }
	}
}